﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AOP.Core
{
    public class ProxyFactory
    {
        #region Proxied Target
        public static T Wrap<T>(T target)
        {
            return (T)(WrapRemotingProxy<T>(target).GetTransparentProxy());
        }

        public static T Create<T>(params object[] args)
            where T : MarshalByRefObject 
        {
            RemotingProxy<T> remotingProxy = CreateRemotingProxy<T>(args);
            return remotingProxy == null ? null : (T)remotingProxy.GetTransparentProxy();
        }
        #endregion

        #region Remoting Proxy
        public static RemotingProxy<T> WrapRemotingProxy<T>(T target)
        {
            return new RemotingProxy<T>(target);
        }

        public static RemotingProxy<T> CreateRemotingProxy<T>(params object[] args)
            where T:MarshalByRefObject 
        {   
            ConstructorInfo constructor = typeof(T).GetConstructor(Type.GetTypeArray(args));
            if (constructor != null)
            {
                T target = (T)constructor.Invoke(args);
                return new RemotingProxy<T>(target);
            }
            return null ;
        }
        #endregion
    }
}
